package com.yongjie.springbootlogin.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录拦截器
 */
public class LoginHandlerIntercptor implements HandlerInterceptor {
    //调用方法之前被拦截
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    //在session中取出用户信息
        Object username=request.getSession().getAttribute("username");
        if(username!=null){
            //已经登录过,可以访问主页
            return true;
        }
        //没有登录过
        request.getRequestDispatcher("/login").forward(request,response);
        return false;
    }
}
